package com.leyou.common.auth.interceptor;

import com.leyou.common.constants.UserHolder;
import com.leyou.common.upload.Path;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class UserInterceptor implements HandlerInterceptor {


    @Override //网关请求转发到微服务之前发送请求
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userIdStr = request.getHeader(Path.USER_ID_KEY);
        if(StringUtils.isBlank(userIdStr)){
            log.info("拦截获取userid 失败请认证");
            return true;
        }
        Long userId = Long.valueOf(userIdStr);
        UserHolder.setUserId(userId);
        return true;
    }
}
